package org.tony.utils;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.tony.db.DbUtil;
import org.tony.entity.SubmitData;

/**
 * 接收sql server提交的数据，解析然后保存
 * @author tony
 *
 */
public class ParseSubmitDateForSqlServer extends HttpServlet {

	private static final long serialVersionUID = 2627932171523101134L;

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		this.doPost(request, response);
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8") ;
		String value = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
				+ request.getParameter("valueChange");
		
		PrintWriter out = response.getWriter();
		out.println("TRUE");
		out.flush();
		out.close();

		SubmitData sd = null;
		try {
			System.out.println(value);
			sd = PareseSubmitDataUtils.parseData(value);
			System.out.println(PareseSubmitDataUtils.generateSql(sd));
			DbUtil.execute(PareseSubmitDataUtils.generateSql(sd),
					JdbcHelper.system);
			DbUtil.execute(PareseSubmitDataUtils.generateSql(sd),
					JdbcHelper.backup);
		} catch (Exception e) {
			e.printStackTrace();
		}

	}
}
